package yunplus.io.hchome.display.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * Created by yfsoft on 2016/12/15.
 */

public class PreviewWebview extends WebView {


    //------------------ private const define ----------------------
    private static final String TAG = "PreviewWebview";


    //------------- var define --------------

    final FpmWebViewClient _mWebviewClient = new FpmWebViewClient();

    final FpmChromeClient _mChromeClient = new FpmChromeClient();

    public PreviewWebview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PreviewWebview(Context context) {
        super(context);
        init();
    }

    public PreviewWebview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init(){
        //为前端h5提供一些PC端的接口，如：启用js,file,cache,localstorage等等
        WebSettings settings = getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setAppCacheEnabled(true);
        settings.setDatabaseEnabled(true);
        setWebViewClient(_mWebviewClient);
        setWebChromeClient(_mChromeClient);
    }

    public void runJS(String js){
        this.loadUrl("javascript:" + js);
    }


    public void setHandler(FpmWebViewHandler handler){
        if(_mWebviewClient != null){
            _mWebviewClient.setHandler(handler);
        }
    }

    @Override
    public void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
        Log.d(TAG, baseUrl);
        Log.d(TAG, data);
        super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
    }

}
